package com.atguigu.gulimall.member.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.exception.PhoneExsitException;
import com.atguigu.gulimall.member.exception.UsernameExsitException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 会员
 *
 * @author xiaohuai
 * @email 1477227147@gmail.com
 * @date 2020-05-25 11:08:39
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @Autowired
    private CouponFeignService couponFeignService;


    @RequestMapping("/coupons")
    public R coupons(@RequestParam Map<String, Object> params) {
        MemberEntity entity = new MemberEntity();
        entity.setNickname("占山");

        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member", membercoupons).put("conpons", membercoupons.get("coupons"));
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo) {
        try {
            memberService.regist(vo);
        } catch (PhoneExsitException e) {
            //返回自定义异常
            return R.error(BizCodeEnume.PHONE_EXIT_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIT_EXCEPTION.getMsg());
        } catch (UsernameExsitException e) {
            //返回自定义异常
            return R.error(BizCodeEnume.USER_EXIT_EXCEPTION.getCode(), BizCodeEnume.USER_EXIT_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        MemberEntity member = memberService.login(vo);
        if (member != null) {
            return R.ok().setDta(member);
        } else {
            return R.error(BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }

    }


    /**
     * 社交登录接口
     *
     * @param user
     * @return
     * @throws Exception
     */

    @PostMapping("oauth2/login")
    public R oauth2Login(@RequestBody SocialUser user) throws Exception {
        MemberEntity mem = memberService.login(user);

        if (mem != null) {
            //TODO 登录成功处理
            return R.ok().setDta(mem);
        } else {

            return R.error(BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }
}
